﻿using AutoMapper;
using ERP.Workflow.Designer.Domain;
using ERP.Workflow.Designer.Application.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Volo.Abp.ObjectExtending;
using Volo.Abp.ObjectMapping;
namespace ERP.Workflow.Designer.Application
{
    public class DefinitionApplicationModuleAutoMapperProfile: Profile
    {
        public DefinitionApplicationModuleAutoMapperProfile()
        {

            CreateMap<DefinitionDto, Definition>().AfterMap((s, t) => {
                foreach (var item in s.ExtraProperties)
                {
                    t.ExtraProperties[item.Key] = s.ExtraProperties[item.Key];
                }
            });
            CreateMap<Definition, DefinitionDto>().AfterMap((s, t) => {
                foreach (var item in s.ExtraProperties)
                {
                    t.ExtraProperties[item.Key] = s.ExtraProperties[item.Key];
                }
            }); ;
            CreateMap<NodeDto, Node>().MapExtraProperties().AfterMap((s,t) => {
                foreach (var item in s.ExtraProperties)
                {
                    t.ExtraProperties[item.Key] = s.ExtraProperties[item.Key];
                }
            });
            CreateMap<PortDto, Port>();
            CreateMap<LinkDto, Link>().AfterMap((s, t) => {
                foreach (var item in s.ExtraProperties)
                {
                    t.ExtraProperties[item.Key] = s.ExtraProperties[item.Key];
                }
            }); ;
            CreateMap<LinkLabelDto, LinkLabel>();
            CreateMap<LinkVertexDto, LinkVertex>();


            CreateMap<DefinitionSaveDto, Definition>()
                .ForMember(t => t.Id, s => s.MapFrom(m => m.Definition.Id))
                .ForMember(t => t.Name, s => s.MapFrom(m => m.Definition.Name))
                .ForMember(t => t.Version, s => s.MapFrom(m => m.Definition.Version))
                .AfterMap((s, t) =>
                {
                    foreach (var item in s.Definition.ExtraProperties)
                    {
                        t.ExtraProperties[item.Key] = s.Definition.ExtraProperties[item.Key];
                    }
                });

            CreateMap<Definition, DefinitionSaveDto>()
                 .AfterMap((s, t) =>
                 {
                     t.Definition = new DefinitionDto
                     {
                         Id = s.Id,
                         Name = s.Name,
                         Version = s.Version,
                     };
                     foreach (var item in s.ExtraProperties)
                     {
                         t.Definition.ExtraProperties[item.Key] = s.ExtraProperties[item.Key];
                     }
                 });

            CreateMap<LinkLabel, LinkLabelDto>();
            CreateMap<  LinkVertex, LinkVertexDto>();
            CreateMap<  Port, PortDto > ();
            CreateMap<Link, LinkDto>().AfterMap((s, t) =>
            {
                foreach (var item in s.Definition.ExtraProperties)
                {
                    t.ExtraProperties[item.Key] = s.Definition.ExtraProperties[item.Key];
                }
            }); ;
            CreateMap<Node, NodeDto>().AfterMap((s, t) =>
            {
                foreach (var item in s.Definition.ExtraProperties)
                {
                    t.ExtraProperties[item.Key] = s.Definition.ExtraProperties[item.Key];
                }
            });

        }
       

    }
}
